package J20241104_high_precision;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description: 高精度乘法
 * User: 王圆豪
 * Date: 2024-11-05
 * Time: 19:03
 */
public class High_precision_multiplication {
    public static void main(String[] args) {
        String s1 = "123";
        String s2 = "21561";

        int[] arr1 = new int[s1.length()];
        int[] arr2 = new int[s2.length()];

        for (int i = s1.length() - 1; i > -1; i--) {
            arr1[s1.length() - i - 1] = s1.charAt(i) - '0';
        }
        for (int i = s2.length() - 1; i > -1; i--) {
            arr2[s2.length() - i - 1] = s2.charAt(i) - '0';
        }


        int[] arr = mul(arr1, arr2);
        int as = arr1.length + arr2.length;
        while(arr[as - 1] == 0 && as > 1){
            as--;
        }
        List<Integer> l = new ArrayList<>();
        for (int i = 0; i < as; i++) {
            l.add(arr[i]);
        }
        Collections.reverse(l);

//        System.out.println(Arrays.toString(arr1));
        System.out.println(l.toString());
    }

    private static int[] mul(int[] arr1, int[] arr2) {
        int[] arr = new int[1000];
        for (int i = 0; i < arr1.length; i++) {
            for (int j = 0; j < arr2.length; j++) {
                arr[i + j] += arr1[i] * arr2[j];
                arr[i + j + 1] += arr[i + j] / 10;
                arr[i + j] = arr[i + j] % 10;
            }
        }

        return arr;
    }
}
